Declarative pinning

ABSTRACT

A system and method is provided for efficiently pinning references to the managed heap. The system and method allow for references to managed objects to be declared as pinned during a call to unmanaged code. The references are then reported as pinned to a garbage collector in response to invocation of a garbage collection service. The reference can be declared as pinned by a programmer within the source code or automatically invoked at run-time based on an unsafe condition. The garbage collection service will not move or relocate objects that are referenced by the active pinned local variables. If a garbage collection does not occur, the fact that the local variables are declared pinned is ignored.

CROSS REFERENCE TO RELATED APPLICATIONS

This application is a divisional U.S. patent application Ser. No.10/865,544 filed Jun. 10, 2004, entitled “DECLARATIVE PINNING” of whichis a divisional application of U.S. patent application Ser. No.09/873,803 filed Jun. 4, 2001, entitled “DECLARATIVE PINNING,” which isa continuation-in-part (CIP) application of U.S. patent application Ser.No. 09/803,240 filed Mar. 8, 2001, entitled “MANIPULATING INTERIORPOINTERS ON A STACK.” This application is also related to U.S. patentapplication Ser. No. 10/865,546, filed Jun. 10, 2004, entitled“DECLARATIVE PINNING” and U.S. patent application Ser. No. 11/034,158,filed Jan. 12, 2005, entitled “DECLARATIVE PINNING.” The entireties ofthese applications are incorporated herein by reference.

TECHNICAL FIELD

The present invention relates to the management of memory in computersystems, and more particularly to a system and method for automaticmanagement of memory employing a garbage collector.

BACKGROUND OF THE INVENTION

Memory available for task execution is one of the most importantresources in a computer system. Therefore, much time and energy has beendirected to efficient utilization and management of memory. An importantaspect of memory management is the manner in which memory is allocatedto a task, deallocated and then reclaimed for use by other tasks. Theprocess that dynamically manages the memory is referred to as the memorymanager. The memory that the memory manager manages is referred to asthe heap. When a program needs a block of memory to store data, theresource sends a request to the memory manager for memory. The memorymanager then allocates a block of memory in the heap to satisfy therequest and sends a reference (e.g., a pointer) to the block of memoryto the program. The program can then access the block of memory throughthe reference.

Memory allocation and deallocation techniques have become very importantin structured programming and object oriented programming languages.Memory allocated from a heap can be used to store information. Oftenthis information is an instantiated object within an objected orientedparadigm. Conventionally, many programming languages have placed theresponsibility for dynamic allocations and deallocation of memory on theprogrammer. These programming language types are referred to asunmanaged or unsafe programming languages, because pointers can beemployed anywhere in an object or routine. In C, C++ and the Pascalprogramming languages, memory is allocated from the heap by a callprocedure, which passes a pointer to the allocated memory back to thecall procedure. A call to free the memory is then available todeallocate the memory. However, if a program overwrites a pointer, thenthe corresponding heap segment becomes inaccessible to the program. Anallocated heap segment may be pointed to by several pointers, located onthe stack or in another allocated heap segment. When all the pointersbecome overwritten, the heap segment becomes inaccessible. A programcannot retrieve from or write data to an inaccessible heap segment.These inaccessible heap segments are known as memory leaks.

Furthermore, dynamically allocated storage may become unreachable if noreference, or pointer to the storage remains in the set of rootreference locations for a given computation. The “root set” is a set ofnode references such that the referenced node must be retainedregardless of the state of the heap. A node is a memory segmentallocated from the heap. Nodes are accessed through pointers. A node isreachable if the node is in the root set or referenced by a reachablenode. Similarly, storage associated with a memory object can bedeallocated while still referenced. In this case, a dangling referencehas been created. In most programming languages, heap allocations arerequired for data structures that survive the procedure that createdthem. If these data structures are passed to further procedures orfunctions, it may be difficult or impossible for the programmer orcompiler to determine the point at which it is safe to deallocate them.Memory objects that are no longer reachable, but have not been freed arecalled garbage.

Due to the above difficulties with reclamation of heap-allocatedstorage, automatic reclamation is an attractive alternative for dynamicmemory management. The automatic identification and reclaiming ofinaccessible heap segments is known as garbage collection. Garbagecollection methodologies determine when a memory segment is no longerreachable by an executing program either directly or through a chain ofpointers. When a memory segment is no longer reachable, the memorysegment can be reclaimed and reused even if it has not been explicitlydeallocated by the program. Garbage collection is particularlyattractive to managed or functional languages (e.g., JAVA, Prolog, LispSmalltalk, Scheme). In some circumstances, managed data structures needto be passed to unmanaged code (e.g., a file read API provided by theoperating system). In these situations, the unmanaged code is unaware ofthe managed constraints. Therefore, there must be some mechanism inplace to assure that the managed data structures are not moved by thegarbage collector, while unmanaged code is manipulating memory managedby the garbage collector.

One conventional mechanism consists of copying the managed datastructure into an unmanaged unmoveable memory. The call to unmanagedcode is made and then the unmanaged memory is copied back into themanaged data structure. This mechanism is inefficient due to theconstant copying back and forth of the managed data structure. Anothermechanism is that the garbage collector is prevented from running whilethe unmanaged call is in progress. This mechanism does not work well inmultithreaded environments. Another conventional mechanism is toallocate unmoveable managed objects with a special API. However, thecreation of these objects is generally slower than other managed objectsand is also difficult for the developer to know which objects will bepassed to an unmanaged API.

SUMMARY OF THE INVENTION

The following presents a simplified summary of the invention in order toprovide a basic understanding of some aspects of the invention. Thissummary is not an extensive overview of the invention. It is intended toneither identify key or critical elements of the invention nor delineatethe scope of the invention. Its sole purpose is to present some conceptsof the invention in a simplified form as a prelude to the more detaileddescription that is presented later.

A system and method is provided for efficiently pinning references tothe managed heap. The system and method allow for references (e.g.,local variables) to managed objects to be declared as pinned inunmanaged code during a call to the unmanaged code. The references arethen reported as pinned to a garbage collector in response to invocationof a garbage collection service. The garbage collection service will notmove or relocate objects that are referenced by active pinnedreferences. If a garbage collection does not occur, the fact that thereferences are pinned is ignored. The reference can be declared aspinned by a programmer within the source code or automatically invokedat run-time in response to an unsafe condition.

In one aspect of the invention, code is executed by an execution engine(e.g., compiler or interpreter) and references (e.g., object referencesand interior references) are stored in an execution stack. The code maybe precompiled, compiled in real-time or interpreted. During executionof a call, local variables on the stack reference objects residing in amanaged heap. Dynamic memory management (e.g., a garbage collectionservice) is employed periodically to cleanup dead or unused objects fromthe stack and/or a heap containing globally shared objects or the like.Local variables that point to managed memory can be declared as beingpinned. This declaration does not change the instructions that willexecute, but does indicate that any object pointed to by this variablecannot move as long as the variable exists. Any structures pointed to bysuch pinned variables can be safely passed as parameters to unmanagedcode.

For example, when a reference to a managed object is passed to anunmanaged call, the object is pinned by having a local variable withinthe unmanaged code declared to be pinned point to the managed object. Ifa garbage collection occurs during invocation of unmanaged code, theexecution engine or a code manager passes the local variables to thegarbage collector as being pinned. The garbage collection service willnot move or relocate objects that are referenced by the active pinnedlocal variables. If a garbage collection does not occur, the fact thatthe local variables are pinned is ignored.

A system and method is provided for executing both managed and unmanagedcode in a managed environment and managing memory employing a garbagecollection system or service. Managed code is code that manipulatesobjects that were allocated in the heap. Managed code is required tohave a mechanism for enumerating all the live garbage collector pointerscurrently in use. Unmanaged code is code that does not manipulategarbage collector pointers and does not need to have a garbage collectorenumeration mechanism. The code may be precompiled, compiled inreal-time or interpreted. Managed code includes information about whichlocal variables point at the managed heap and whether they should bepinned or not. During execution the local variables are stored on astack. Upon a call to unmanaged code, references pointing to managedobjects can be programmatically or automatically declared pinned by aprogrammer, by an execution engine, a compiler or the like.

For example, local variables of methods of unmanaged code referencingmanaged objects are declared pinned prior to a garbage collection. Thepinned references are then stored on the stack. The system and methodidentify roots including object references, interior references andpinned references on a stack. The references are reported to a garbagecollection system or service. The garbage collection system or serviceemploys the references when tracing the heap for objects and datamembers (e.g., integer numbers, floating values, character fields, otherobjects) within the objects. Memory that is inaccessible is thenreclaimed for assignment to other objects. Objects referred to by pinnedreferences are fixed in their current location during any garbagecollection.

In one aspect of the invention, a system and method is provided forpinning local variables of unmanaged code referencing managed objectsduring execution of code in a run-time environment. Upon invocation of agarbage collection service, the local variables are passed to thegarbage collections service as pinned. Objects referenced by pinnedlocal variables are then fixed during reclamation of inaccessible memorysegments.

In another aspect of the present invention, native code is generated bya compiler (e.g., Just-In-Time compiler) and object references andinterior references are stored in a process stack. The code can includeboth managed and unmanaged code. The interior references (e.g.,references within an object) are created and stored on the stack bycalls within the code. For example, a call to modify a field or datamember within an object may be made within the code. In response, thecompiler creates an interior reference, which is stored in the processstack. A reference can be declared pinned by the compiler and stored inthe stack. A reference can be declared pinned if the compiler decides itis unsafe to move objects referenced by the pinned reference duringexecution of a call. A code manager then scans the stack and passes theobject references, interior references and the pinned references to agarbage collector. The garbage collector then employs the objectreferences, the interior references and pinned references to trace theheap, remove inaccessible objects, and move accessible objects notreferenced by pinned references.

To the accomplishment of the foregoing and related ends, the inventionthen, comprises the features hereinafter fully described andparticularly pointed out in the claims. The following description andthe annexed drawings set forth in detail certain illustrativeembodiments of the invention. These embodiments are indicative, however,of but a few of the various ways in which the principles of theinvention may be employed and the present invention is intended toinclude all such embodiments and their equivalents. Other objects,advantages and novel features of the invention will become apparent fromthe following detailed description of the invention when considered inconjunction with the drawings.

BRIEF DESCRIPTION OF THE DRAWINGS

FIG. 1 illustrates a block diagram of a machine having a memory managersystem in accordance with one aspect of the present invention.

FIG. 2 illustrates a block diagram of a memory heap in accordance withone aspect of the invention.

FIG. 3 illustrates a block diagram of a machine having components fortracing references in accordance with one aspect of the presentinvention.

FIG. 4 illustrates a flow diagram of a methodology of declaringreferences as pinned prior to invocation of a garbage collection servicein accordance with one aspect of the present invention.

FIG. 5 illustrates a flow diagram of a methodology of reclaiminginaccessible memory segments in a heap in accordance with one aspect ofthe present invention.

FIG. 6 illustrates a block diagram of components of a system inaccordance with one aspect of the present invention.

FIG. 7 illustrates a block diagram of various programs and components ona machine in accordance with one aspect of the present invention.

FIG. 8 illustrates a flow diagram of a methodology for executing code bya JIT compiler and reclaiming inaccessible memory segments in a heap inaccordance with one aspect of the invention.

FIG. 9 illustrates a block diagram of a computer system in accordancewith an environment of the present invention.

DETAILED DESCRIPTION OF THE INVENTION

The present invention is now described with reference to the drawings.General purpose programming languages and other programmed systems oftenuse references to locate and access objects. These objects can containreferences to data members, such as integers, floating point numbers,character fields or other objects. These objects can be either managedor unmanaged. During execution of unmanaged code, managed datastructures can be used. The present invention declares references tomanaged objects or data structures as pinned during execution ofunmanaged calls.

The present invention is described with reference to a system and methodfor efficiently pinning references to a managed heap. The system andmethod allow for references (e.g., local variables) to managed objectsto be declared as pinned during unsafe conditions (e.g., a call tounmanaged code that manipulates managed data). Upon invocation of agarbage collection, the references and pinned references are passed to agarbage collection system or service. The garbage collection system orservice employs both the references and pinned references when tracingthe heap for objects. Objects that are inaccessible are then reclaimedfor assignment to other objects. However, objects referenced by pinnedreferences are not moved during reclaiming of memory.

In one aspect of the invention, some arguments or local variables aredeclared to be pinned in the source code. In another aspect of theinvention, some arguments or local variables are declared pinnedautomatically by an execution engine or the like. The pinned referencesare only allowed on the stack and not in the heap. The references arepinned for the lifetime of the local variable on the stack. For example,when a pointer to a managed object is passed to an unmanaged call, theobject is pinned by having a local variable declared to be pinned pointto it. If a garbage collection occurs, this local variable is reportedto the garbage collector as being pinned. The garbage collection systemor service may be invoked periodically by an operating system, a memorymanager or some other service. Alternatively, the garbage collectionsystem can be invoked in response to a request for memory by anexecuting program. The garbage collections system can also be invokedwhen the heap becomes full.

FIG. 1 illustrates an example of a machine 10 including a process stack12, an execution engine 14, a memory manager 15, and a memory heap 20.The memory manager includes a code manager 16 and a garbage collector18. During execution of one or more programs or processes on a machine,status information about executing programs are stored on the processstack 12, so that programs can call one another. When one program callsanother program, status information about the calling program is storedon the stack 12, so that the calling program can resume its processingafter the called program returns control of the processor to the callingprogram. The status information also includes processor register valuesof the process at the time the process relinquished control of theprocessor, object and variable declaration and calls and intermediatecomputational quantities, such as object references and interiorreferences to the objects referenced.

In the example of FIG. 1, the stack 12 includes, amongst other items, anumber object references (e.g., pointers) that reference the location ofa corresponding object in the memory heap 16. The objects can be groupedinto frames (not shown). Each frame corresponds to one level ofsubroutine invocation or method invocation. The stack 12 also includes anumber of interior references (e.g., pointers) that reference thelocation of data members (e.g., integer numbers, floating values,character fields, other objects) within the various objects. Since anynumber of programs may call the same objects and/or object data members,multiple references or interior references to an object can reside inthe process stack 12. The object reference can also be located inanother heap segment of the memory heap 20. The interior referencelocations can be limited to residing on the stack.

The execution engine 14 runs both managed and unmanaged code. Managedcode is code reports its garbage collection heap references, whileunmanaged code does not report its heap references and does not employgarbage collection. When a program needs memory to store data, theexecution engine 14 allocates an object in the garbage collection heap20 to satisfy the request. The execution engine 14 then sends areference to the object to the program. The program can then access theobject through the reference. The execution engine 14 declaresreferences (e.g., local variables) to managed data structures or objectsas being pinned during unsafe conditions (e.g., a call to unmanaged codethat manipulates managed data). Additionally, references can be declaredas pinned in the source code to allow for fine grain control by theprogrammer. Declaring references pinned has no effect on thefunctionality of the system 10, unless a garbage collection occurs.

The execution engine 14 will invoke or receive an invocation to performa garbage collection algorithm. Alternatively, the invocation can be aresult of a request for memory by a program. Upon invocation of thegarbage collection algorithm, the execution engine 14 will reportcertain references as pinned, to assure the integrity of the program.For example, local variables can be declared to be pinned, whichindicates that the object they point to will not move over the lifetimeof the local variable. Thus, the object being pointed to by the localvariable can be passed to a call to unmanaged code. The execution engine14 will then invoke the code manager 16, which will scan or decode thestack 12 for the roots on the stack (e.g., object references, interiorreferences, pinned object references and pinned interior references).The code manager 16 then transmits the roots to the garbage collectionsystem 18.

Although the code manager 16 is illustrated as a separate component fromthe garbage collection system 18 within the memory manager system 15, itis to be appreciated that the code manager 16 can be integrated into thegarbage collection system 18. The garbage collection system will thentrace through the memory heap 20 to determine accessible objects usingthe transmitted references. The garbage collector 18 will perform aclean up of the memory heap 20, which comprises moving objects that aretraceable and removing objects that are not traceable. However, thegarbage collector 18 will not move objects that are referenced by pinnedreferences. Every program or application executing on the machine 10 hasa set of roots. Roots identify storage locations, which refer to objectson the managed heap. All of the global and static object pointers in aprogram are considered part of the program's roots. In addition, anylocal variable/parameter object pointers on a thread's stack areconsidered part of the program's roots. Furthermore, any CPU registerscontaining pointers to objects in the managed heap are also consideredpart of the program's roots.

FIG. 2 illustrates a block diagram of the contents of a memory heap 30.A plurality of objects reside in the memory heap 30. Each objectincludes a header and a body. The contents of the object headers arecontrolled by the operating system and are generally not accessible tousers and application programs. The object header contains or refers toinformation that supports the instantiated object. The information inthe object header often includes a pointer to a class definition, amethods table and an instance-variable count. The object referencesprovide information on the locations of the object header. The objectbody is the part of the object that is visible to application programsand includes the variables and various fields used by the applicationprograms. A first object 42 includes an object header 32 and an objectbody 33. A second object 44 includes an object header 34 and an objectbody 35. The object body 35 includes a first and a second data member.The data members can be integers, floating point numbers, characterfields, data structures or pointers to other objects. In the presentinvention, object references can be found in the stack that referencethe object headers, while interior references can be found in the stackthat reference the data members within the object body. A third object46 includes an object header 36 and an object body 37. The object body37 includes a third data member. A fourth object 48 includes an objectheader 38 and an object body 39. The object body 39 includes a fourth, afifth and a sixth data member.

As previously stated, the garbage collection system 18 will tracethrough the memory heap or walk the roots utilizing the objectreferences and interior references to determine if objects referenced bythe references are accessible. The garbage collector will build a graphof all object references reachable from the roots.

For example, if the garbage collection system 18 traces object reference#1 to the first object 42 in the heap 30, the garbage collection systemwill add the first object 42 to the graph. Additionally, if the garbagecollection system notices that the first object 42 refers to the secondobject 44, then second object 44 is added to the graph. The garbagecollection system continues to walk through all reachable objectsrecursively. If a reference is pinned, the object referred to by thepinned reference is added to the graph, but noted as pinned. The garbagecollection system then reclaims inaccessible memory segments by movingthe objects to the front of the heap that have been located, except forthe objects referred to by pinned references. In none of the referencesare pinned, the garbage collection system moves all of the objects up inthe heap.

For example, as illustrated in FIG. 3, a machine 50 is provided thatincludes a stack 52, a code manager 54, a garbage collection service 58and a heap 62. The heap 62 is logically divided into equal sized bricks64 (e.g., 2 KB) and a brick table 60 is set up to contain for eachbrick, the address of an object within that brick. The code manager 54decodes the stack 52 for references (e.g., object references andinterior references) and pinned references (e.g., pinned objectreferences and pinned interior references). The references and pinnedreferences are transmitted to the garbage collection services 58. Thegarbage collection service 58 traces the object references and itsdescendants and marks them in an object trace graph 56. If the referenceis pinned, the garbage collection system notes this in the object tracegraph.

As the garbage collection service 58 walks through objects and datamembers, an attempt to add an object to the graph that was previouslyadded will result in the garbage collection service 58 aborting walkingdown that path. This serves the purpose of significantly improvingperformance since the garbage collection service 58 doesn't walk througha set of objects more than once. Also, it prevents infinite loops if anycircular linked lists of objects exist. Once all roots have beenchecked, the object trace graph 56 contains all the sets of all objectsthat are somehow reachable from the program's roots. The garbagecollection service 58 then can shift the non-garbage objects down inmemory removing all the gaps in the heap 62, except for any object thatis referenced by an element type pinned will remain in its originallocation so that managed data structures will not be moved during anexecution call, for example, to unmanaged code. The garbage collectionservice 58 then modifies the programs roots so that the references pointto the objects new locations. The garbage collection service 58 alsomodifies any references to objects within objects. Pinned references donot need updating since the garbage collector guarantees that the objectthese references point to have not been moved.

In view of the foregoing structural and functional features describedabove, a methodology in accordance with various aspects of the presentinvention will be better appreciated with reference to FIGS. 4-5. While,for purposes of simplicity of explanation, the methodology of FIGS. 4-5is shown and described as executing serially, it is to be understood andappreciated that the present invention is not limited by the illustratedorder, as some aspects could, in accordance with the present invention,occur in different orders and/or concurrently with other aspects fromthat shown and described herein. Moreover, not all illustrated featuresmay be required to implement a methodology in accordance with an aspectthe present invention. It is further to be appreciated that thefollowing methodology may be implemented as computer-executableinstructions, such as software stored in a computer-readable medium.Alternatively, the methodology may be implemented as hardware or acombination of hardware and software.

FIG. 4 illustrates one particular methodology for declaring referencesas pinned prior to invocation of a garbage collection service inaccordance with the present invention. The methodology begins at 85where code begins executing by an execution engine, a compiler or thelike. At 90, managed parameters are passed to an executing call (e.g.,unmanaged code). Some of these passed parameters may have been declaredpinned in the source code. At 95, references to the parameters and orreferences to the managed objects of the parameters are declared aspinned. At 100, it is determined if a garbage collection service isinvoked. If a garbage collection service is not invoked during executionof the call (NO), the call proceeds normally and completes executing at115. If a garbage collection service is invoked during execution of thecall (YES), the method proceeds to 105. At 105, the code manager orexecution engine reports references on the stack, indicating whichreferences are pinned. The method then proceeds to 110 where the garbagecollection service traces the heap and moves managed objects except forobjects being referenced by pinned local variables/parameters. Themethod then advances to 115 and completes executing.

FIG. 5 illustrates a methodology for reclaiming inaccessible memorysegments in a memory heap in accordance with the present invention. Themethodology begins at 130 where local variables/parameters residing inunmanaged code referencing managed data structures or objects aredeclared as pinned. The methodology then proceeds to 135 where a garbagecollection service is invoked. At 135, the process stack is scanned andreferences and pinned references are identified. The garbage collectionservice begins tracing the memory heap to determine live objects andlive data members at 140. The methodology then proceeds to 145 where thegarbage collection service retrieves a reference and determines if areference is a pinned reference. If the reference is not a pinnedreference (NO), the methodology proceeds to 155. If the reference is apinned reference (YES), the methodology proceeds to 150. At 150, thegarbage collector pins objects in the heap referenced by pinned localvariables/parameters. The methodology then proceeds to 155.

At 155, the garbage collection system traces the object and itsdescendants. If the object is found it is added to a trace graph and anyreferences (e.g., descendants) found that were called out within thatobject are also added to the trace graph. At 160, the methodologydetermines if the reference is a final reference. If the reference isnot a final reference (NO), the garbage collection system returns to 145to get another reference and determine if that reference is a pinnedreference. If the reference is a final reference (YES), the methodologyproceeds to 165. At 165, the garbage collection system reclaimsinaccessible memory segments in the heap by moving objects except forpinned objects and then returns control of the operations back to normalexecution.

FIG. 6 illustrates a block diagram of a system 170 in accordance withanother aspect of the present invention. The system 170 includes acentral processing unit 172 (CPU) and a main memory system 171. The mainmemory system 171 includes a number of components and programs residingin the main memory system 171. A process stack 174 resides in the mainmemory 171 for storing program and process states including objectreferences, interior references and pinned references. The main memorysystem 171 also includes a heap 176 for storing program data such asobjects and data members within objects. An operating system 180 resideson the main memory system 171 and provides the mechanism for invokingprograms on the system 170. The main memory 171 also includes anexecution engine 186 that is used via the CPU 172 to compile orinterpret source code 178. The execution engine 186 includes a compiler188 and an interpreter 190. The source code 178 can be written in anynumber of languages including both managed code (e.g., JAVA) andunmanaged code (e.g., C, C++). A code manager 182 and a garbagecollector 184 also reside in the main memory 171. The code manager 182performs various functions including managing the execution of thesource code, storing metadata regarding the source code and keepingtrack of object references, interior references and pinned references inthe stack 174. The garbage collector 184 is operable to receive theseobject references, interior references and pinned references from thecode manager 182 and reclaim inaccessible memory from the heap 176.

During operation of the system 170, the CPU 172 executes code comprisingthe operating system 180. Application programs residing as source code178 can be invoked through the operating system 180. The executionengine 186 reduces the source code 178 to an intermediate form orintermediate language for compiling or interpreting by the compiler 188and interpreter 190, respectively. Compiled or interpreted code can thenbe linked with libraries (not shown) or the like, converted to machinecode or native code and executed by the CPU 172. During execution,various object references and interior references are stored in thestack 174 and various objects are stored in the heap 176. The executionengine 186 is adapted to execute both managed and unmanaged code. If acall to unmanaged code manipulates managed code, data and/or objects,the local variables referencing the managed structure are marked aspinned or defined as element type pinned. The execution engine 186informs the code manager 182 of any garbage collection invocation.

The code manager 182 includes a stack decoder service in addition toother services, such as error handling. The code manager 182 is amechanism for enumerating the garbage collector references that the codeis currently manipulating, which almost resides exclusively on thestack. The other place being the machine registers. The code manager 182then scans the stack and identifies references and pinned references.The code manager 182 then passes these references and pinned referencesto the garbage collector 184. The garbage collector 184 then employs thereferences and pinned references in reclaiming inaccessible memorysegments in the heap 176, as previously described, and holding objectsreferenced by pinned references fixed in their current memory locationuntil termination or completion of the call.

FIG. 7 illustrates a block diagram of the interaction of variousprograms and components residing on a machine 200. The machine 200includes an operating system 210 for providing a general environment tothe machine 200. The operating system 210 is operable to invoke one ormore programs for execution on the machine 200. The operating system 210invokes a Just-In-Time (JIT) compiler 212 to begin compiling source code214. The compiler 212 is operable to compile both managed languages andunmanaged languages. For example, the source code can be precompiled into an intermediate language code prior to compiling by the JIT compiler212. The JIT compiler can be formed of two compilers, one for managedcode and one for unmanaged code. The intermediate language code can thenbe compiled into object code and the managed code and unmanaged codelinked and converted to machine code or native code for execution by themachine 200.

During execution of one or more programs or processes on the machine200, status information about executing programs are stored on a stack202, so that programs can call one another. The status informationincludes both object pointers and interior pointers to data memberswithin the objects referenced. These object pointers and interiorpointers can be declared as pinned by the JIT compiler in the event ofan unsafe condition. For example, in some situations conversion takeplace, so that unmanaged and managed code can communicate. In thisevent, the managed code cannot be moved when managed data is beingmanipulated. The JIT compiler recognizes this situation and declares apointer or reference to the managed structure as pinned. During normalexecution, the fact that the pointer or reference is pinned is ignored.Upon a garbage collection invocation, the object pointers, interiorpointers and pinned object pointers and pinned interior pointers arereported to the garbage collector 208. The garbage collector 208 thenholds objects referenced by the pinned pointers as pinned.

In the example of FIG. 7, the stack 202 includes, amongst other items, anumber of object pointers that reference the location of a correspondingobject in a memory heap 216. The stack 202 also includes a number ofinterior pointers that reference the location of data members (e.g.,integer numbers, floating values, character fields, other objects)within the various objects. The stack 202 also includes a number ofpinned pointers that can be either object pointers or interior pointers.Since any number of programs may call the same objects, an objectpointer, interior pointer and a pinned pointer can reside in the processstack 202 a number of times. The interior pointers and pinned pointersare limited to residing on the stack 202.

A code manager 204 is provided to dynamically manage the heap memory216. The code manager includes a stack decoder service in addition toother services, such as error handling. When a program needs a segmentof memory to store data, the program sends a request to the code manager204. The code manager 204 will inform the JIT compiler 212 of therequest. The JIT compiler 212 will determine which references should bedeclared as pinned prior to a garbage collection operation and markthose references or declare those references as pinned when stored inthe stack 202. During a garbage collection, the code manager 204 scansthe stack 202 for the roots on the stack 202 (e.g., object pointers,interior pointers, pinned pointers). The code manager 204 then reportsthe roots (e.g., object pointers, interior pointers, pinned pointers) toa garbage collector 208 through an application program interface 206(API). The garbage collector 208 will then trace through the memory heap216 to determine inaccessible objects using the object pointers,interior pointers and pinned pointers. The garbage collector 208 thenmoves the accessible objects, fixes those objects from which the pinnedpointers refer and adjusts the pointers accordingly. The JIT compiler212 then provides the program with a memory segment corresponding to therequest.

In view of the foregoing structural and functional features describedabove, a methodology in accordance with various aspects of the presentinvention will be better appreciated with reference to FIG. 8. While,for purposes of simplicity of explanation, the methodology of FIG. 8 isshown and described as executing serially, it is to be understood andappreciated that the present invention is not limited by the illustratedorder, as some aspects could, in accordance with the present invention,occur in different orders and/or concurrently with other aspects fromthat shown and described herein. Moreover, not all illustrated featuresmay be required to implement a methodology in accordance with an aspectthe present invention. It is further to be appreciated that thefollowing methodology may be implemented as computer-executableinstructions, such as software stored in a computer-readable medium.Alternatively, the methodology may be implemented as hardware or acombination of hardware and software.

FIG. 8 illustrates one particular methodology for reclaiminginaccessible memory segment in a heap and assigning a memory segmentbased on a request from a program in accordance with one aspect of thepresent invention. The methodology begins at 300 where the operatingsystem invokes the JIT compiler based on a request for running anapplication program. At 310, the JIT compiler begins compiling sourcecode and the processor begins executing the compiled code. At 320, thecompiler declares certain local variables as pinned based on apredefined criteria to prevent an unsafe condition occurring that mayresult in a failure. At 330, process states including object pointersand interior pointers get stored on the stack during execution. At 340,objects and object data members get stored to the heap during execution.At 350, a garbage collection service is invoked. The code manager scansthe stack and passes references and pinned references to the garbagecollector at 360. At 370, the garbage collector traces the heap andremoves data from inaccessible memory regions, except for objectsreferenced by pinned local variables.

With reference to FIG. 9, an exemplary system for implementing theinvention includes a conventional personal or server computer 420,including a processing unit 421, a system memory 422, and a system bus423 that couples various system components including the system memoryto the processing unit 421. The processing unit may be any of variouscommercially available processors, including Intel x86, Pentium andcompatible microprocessors from Intel and others, including Cyrix, AMDand Nexgen; Alpha from Digital; MIPS from MIPS Technology, NEC, IDT,Siemens, and others; and the PowerPC from IBM and Motorola. Dualmicroprocessors and other multi-processor architectures also can be usedas the processing unit 421.

The system bus may be any of several types of bus structure including amemory bus or memory controller, a peripheral bus, and a local bus usingany of a variety of conventional bus architectures such as PCI, VESA,Microchannel, ISA and EISA, to name a few. The system memory includesread only memory (ROM) 424 and random access memory (RAM) 425. A basicinput/output system (BIOS), containing the basic routines that help totransfer information between elements within the computer 420, such asduring start-up, is stored in ROM 424.

The computer 420 further includes a hard disk drive 427, a magnetic diskdrive 428, e.g., to read from or write to a removable disk 429, and anoptical disk drive 430, e.g., for reading a CD-ROM disk 431 or to readfrom or write to other optical media. The hard disk drive 427, magneticdisk drive 428, and optical disk drive 430 are connected to the systembus 423 by a hard disk drive interface 432, a magnetic disk driveinterface 433, and an optical drive interface 434, respectively. Thedrives and their associated computer-readable media provide nonvolatilestorage of data, data structures, computer-executable instructions, etc.for the server computer 420. Although the description ofcomputer-readable media above refers to a hard disk, a removablemagnetic disk and a CD, it should be appreciated by those skilled in theart that other types of media which are readable by a computer, such asmagnetic cassettes, flash memory cards, digital video disks, Bernoullicartridges, and the like, may also be used in the exemplary operatingenvironment.

A number of program modules may be stored in the drives and RAM 425,including an operating system 435, one or more application programs 436,other program modules 437, and program data 438. A user may entercommands and information into the computer 420 through a keyboard 440and pointing device, such as a mouse 442. Other input devices (notshown) may include a microphone, joystick, game pad, satellite dish,scanner, or the like. These and other input devices are often connectedto the processing unit 421 through a serial port interface 446 that iscoupled to the system bus, but may be connected by other interfaces,such as a parallel port, game port or a universal serial bus (USB). Amonitor 447 or other type of display device is also connected to thesystem bus 423 via an interface, such as a video adapter 448. Inaddition to the monitor, computers typically include other peripheraloutput devices (not shown), such as speakers and printers.

The computer 420 may operate in a networked environment using logicalconnections to one or more remote computers, such as a remote server orclient computer 449. The remote computer 449 may be a workstation, aserver computer, a router, a peer device or other common network node,and typically includes many or all of the elements described relative tothe computer 420, although only a memory storage device 450 has beenillustrated in FIG. 9. The logical connections depicted in FIG. 9include a local area network (LAN) 451 and a wide area network (WAN)452. Such networking environments are commonplace in offices,enterprise-wide computer networks, intranets and the Internet.

When used in a LAN networking environment, the computer 420 is connectedto the local network 451 through a network interface or adapter 453.When used in a WAN networking environment, the server computer 420typically includes a modem 454, or is connected to a communicationsserver on the LAN, or has other means for establishing communicationsover the wide area network 452, such as the Internet. The modem 454,which may be internal or external, is connected to the system bus 423via the serial port interface 446. In a networked environment, programmodules depicted relative to the computer 420, or portions thereof, maybe stored in the remote memory storage device. It will be appreciatedthat the network connections shown are exemplary and other means ofestablishing a communications link between the computers may be used.

In accordance with practices of persons skilled in the art of computerprogramming, the present invention is described below with reference toacts and symbolic representations of operations that are performed bythe computer 420, unless indicated otherwise. Such acts and operationsare sometimes referred to as being computer-executed. It will beappreciated that the acts and symbolically represented operationsinclude the manipulation by the processing unit 421 of electricalsignals representing data bits which causes a resulting transformationor reduction of the electrical signal representation, and themaintenance of data bits at memory locations in the memory system(including the system memory 422, hard drive 427, floppy disks 429, andCD-ROM 431) to thereby reconfigure or otherwise alter the computersystem's operation, as well as other processing of signals. The memorylocations where data bits are maintained are physical locations thathave particular electrical, magnetic, or optical propertiescorresponding to the data bits.

The present invention has been illustrated with respect to a programmingmethodology and/or computer architecture and a particular example,however, it is to be appreciated that various programming methodologyand/or computer architecture suitable for carrying out the presentinvention may be employed and are intended to fall within the scope ofthe hereto appended claims.

The invention has been described with reference to the preferred aspectsof the invention. Obviously, modifications and alterations will occur toothers upon reading and understanding the foregone detailed description.It is intended that the invention be construed as including all suchmodifications alterations, and equivalents thereof.

1. A computer-readable medium having computer-executable instructionsfor performing the steps comprising: executing code corresponding to atleast one program; storing objects on a memory heap during execution ofthe code; storing references to the objects in a process stack duringexecution of the code; declaring at least one reference as pinned andstoring the at least one pinned reference on the process stack; invokinga garbage collection algorithm; scanning the stack to identify theobject references and the at least one pinned reference in response toinvocation of the garbage collection algorithm; tracing the heap forinaccessible objects corresponding to the object references and the atleast one pinned reference; removing objects that have been determinedto be inaccessible; and relocating objects in the heap except forobjects referenced by the at least one pinned reference.
 2. The computerreadable medium of claim 1, the code being both managed and unmanagedcode.
 3. The computer readable medium of claim 2, further comprisingcompiling source code by a just-in-time compiler in real-time prior toexecuting the code.
 4. The computer readable medium of claim 3, whereinscanning the stack to identify the object references and at least onepinned reference is performed by a code manager.
 5. The computerreadable medium of claim 4, the code manger reporting the objectreferences and at least one pinned reference to the garbage collectionservice, wherein the garbage collection service traces the heap forinaccessible objects corresponding to the object references and at leastone pinned reference and removes objects that have been determined to beinaccessible.